﻿toggle = function (fsid)
{
    var div = $id("div_" + fsid);
    var btnAdd = $id("btnAdd" + fsid);
    var load = $id("load_" + fsid);
    var lnkdscd = $id("lnkdscd__lov_" + fsid);
    var lnkedit = $id("lnkedit__lov_" + fsid);
    var display = "";
    
    if( div.style.display != "none" )
    {
        display = "none";
    }
    
    div.style.display = display;
    
    if (btnAdd != null)
    {
        btnAdd.style.display = display;
    }
    if (load != null)
    {
        load.style.display = display;
    }
    if(lnkdscd != null)
    {
        lnkdscd.style.display = display;
    }
    if(lnkedit != null)
    {
        lnkedit.style.display = display;
    }
}
AddLog = function(eventId)
{
    var div = $id("popuplayer");    div.style.display = "block";    div.innerHTML = "<img src='" + LOADING.src + "' alt='' />";    renderFirst();        cbkLog_c.PerformCallback("LOG" + DELIMITER + eventId); 
}
cancelLog = function(eventId)
{
    var input = getLogData(eventId, "CANCEL");
    if(input)
    {
        cbkLog_c.PerformCallback(input);
    }
    closeLog();
}
closeLog = function(){
    var div = $id("popuplayer");    div.innerHTML = "";    div.style.display = "none";
}
addMore = function(eventId)
{
    var hf = $id("hf.eventlogcount");
    //var evtId = $id("hf.eventid");
    cbkLog_c.PerformCallback("MORE" + DELIMITER + eventId + DELIMITER + hf.innerHTML);    
}
saveLog = function(eventId)
{
    var load = $id("load_" + eventId);
    var hf = $id("hf.eventlogcount");
    if(confirm("Save Log?"))
    {
        var input = getLogData(eventId, "SAVE");
        load.innerHTML = "<img src='" + LOADING.src + "' alt='' />";
        cbkLog_c.PerformCallback(input);
    }
}
getLogData = function(eventId, mode)
{
    var inputs = $nm("txt");
    var iNotUploadedCount = 0;
    var iEmptyValueCount = 0;
    var output = mode + DELIMITER + eventId + DELIMITER;
    
    for( var i=0; i < inputs.length; i++ )
    {
        if( i > 0 )
        {
            output += DELIMITER;
        }
        
        var item = inputs[i];
        var lov = $id("lov_" + item.id);
        
        if( lov == null )
        {
            output += NEWLINE + item.value;
            if(item.value == "")
            {
                iEmptyValueCount++;
                item.style.backgroundColor = "#FFFFFF";
            }
            var uplbtn = $id("uplbtn_" + item.id);
            if(uplbtn != null)
            {
                iNotUploadedCount++;
                item.style.backgroundColor = "#FFFFFF";
            }
        }
//        else
//        {
//            var lovh = $id("lovh_" + item.id);
//            output += lov.value + NEWLINE + lovh.value + NEWLINE + item.value;        
//        }
    }
    var sxError = "";
    
    if( iNotUploadedCount > 0 )
    {
        sxError += "terdapat file yang belum diupload! ";
    }
    
    if( iEmptyValueCount > 0 )
    {
        sxError += "terdapat field isian yang belum diisi! ";
    }
    
    if( sxError != "" )
    {
        //alert(sxError);
        return false;
    }
    
    return output;
}
cbkLog_c_CallbackComplete = function(e){
    if( e.parameter.startsWith("LOG") )
    {
        var div = $id("popuplayer");
        div.innerHTML = e.result;    }
    else if( e.parameter.startsWith("MORE") )
    {
        var eventId = e.parameter.split(DELIMITER)[1];
        var div = $id("div_log_" + eventId);        var hf = $id("hf.eventlogcount");
        
        hf.innerHTML = parseInt(hf.innerHTML) + 1;
        
        if( div.insertAdjacentHTML )
        {
            div.insertAdjacentHTML( "beforeEnd", e.result );
        }
        else
        {
            var div2 = document.createElement('div');
            div2.innerHTML = e.result;
            div.appendChild(div2);
        }
    }
    else if( e.parameter.startsWith("SAVE"))
    {
        closeLog();
    }
}

